package com.hyy.libray.ui.fragment;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyy.libray.R;
import com.hyy.libray.contract.BaseLoadMoreContract;
import com.hyy.libray.presenter.BaseLoadMorePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @ClassName: com.mala.app.live_teacher.ui.fragment
 * @Description:
 * @Author: hyy
 * @Date: 2019/6/26
 * @Time: 3:12 PM
 */
public abstract class BaseLoadMoreFragment<T extends BaseLoadMorePresenter> extends BaseFragment<T> implements BaseLoadMoreContract.IView {

    private RecyclerView mRecyclerView;

    private SmartRefreshLayout smartRefreshLayout;


    public BaseQuickAdapter mCommonRecycleViewAdapter;

    public RecyclerView.LayoutManager mLayoutManager;

    @Override
    public void initView(View view) {
        onInitView(view);
        mRecyclerView = view.findViewById(R.id.recycle_view);

        smartRefreshLayout = view.findViewById(R.id.smart_refresh);

        if (mRecyclerView != null) {

            mLayoutManager = onLayoutManager();
            if (mLayoutManager == null) {

                mLayoutManager = new LinearLayoutManager(getContext());
            }

            mCommonRecycleViewAdapter = onCommonRecycleViewAdapter();

            if (mCommonRecycleViewAdapter == null) {

                throw new RuntimeException("onCommonRecycleViewAdapter返回不能为空");
            }


            mRecyclerView.setLayoutManager(mLayoutManager);

            mRecyclerView.setAdapter(mCommonRecycleViewAdapter);


        }

        if (smartRefreshLayout != null) {
            smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
            smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
            smartRefreshLayout.setEnableLoadMore(isLoadMore());
            smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    mPresenter.getListDatas(false);
                }

                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    mPresenter.getListDatas(true);
                }
            });
        }

    }


    public boolean isLoadMore() {
        return true;
    }

    public abstract void onInitView(View view);

    /**
     * recyecleview，适配器
     *
     * @return
     */
    public abstract BaseQuickAdapter onCommonRecycleViewAdapter();

    /**
     * recyecleview，LayoutManager
     *
     * @return 默认LinearLayout.VERTICAL
     */
    public RecyclerView.LayoutManager onLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    @Override
    public void setListDatas(boolean isRefresh, boolean isLoadMore, List itemDatas) {
        if (isRefresh) {
            onFinishRefresh();
            if (itemDatas != null) {
                mCommonRecycleViewAdapter.setNewData(itemDatas);
            }
        } else {
            if (itemDatas != null) {
                mCommonRecycleViewAdapter.addData(itemDatas);
            }
        }

        if (isLoadMore) {
            onFinishLoadMore();
        } else {
            onStopLoadMore();
        }
    }

    /**
     * 暂定取消
     */
    public void onFinishRefresh() {

        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh();
        }

    }

    /**
     * 暂定上拉加载更多
     */
    public void onFinishLoadMore() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadMore();
        }

    }

    /**
     * 取消上拉加载更多
     */
    public void onStopLoadMore() {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadMoreWithNoMoreData();
        }
    }


}
